[ Home ] [ Authors ] [ Index ] [ Abbreviations ] [ Key bindings ]

The FontManager Class

Examining Fonts

The FontManager class in Tycho provides a top-level window within which the user can select a font, together with a set of utilities for controling X window fonts. The simplest use of the class is as follows:

::tycho::FontManager .fontmanager
.fontmanager centerOnScreen
This creates an interactive window that permits the user to try out the available fonts. Rather than exposing to the user the arcane font specification that is used by the X window system, this window provides a set of pull-down menus that can be used to select fonts by family, size, weight, and style. Since some of the best X window fonts do not conform to this partitioning, a separate menu is provided for selecting miscellaneous fonts by name. For example, the 9x15 and 10x20 fonts are particularly nice screen fonts. When the user selects a font, a sample is displayed.

Setting Fonts Interactively

The FontManager class can be used to set fonts in any Tycho text widget. Opening a FontManager with a specified -okcommand option is one way to do this. For example, the following code will query the user for a font selection, and then post the font name in a new window when the user clicks the OK button:

::tycho::FontManager .fm \
        -okcommand "::tycho::post \[.fm getCurrentFont\]"
.fm centerOnScreen
The backslashes in front of the square brackets are needed to defer the evaluation of the getCurrentFont method until the OK button is pushed.

Font Utilities

For the convenience of applications that wish to directly control fonts, without user intervention, an instance of the FontManager class is created when Tycho starts up in tycho namespace. This instance, called .tychoFonts, is never mapped to the screen. By interacting with it through its public members, however, an application can check the validity of a font, find a font given a prioritized list, or query for the default fonts used in Tycho (which can vary by installation, since X window fonts are not standardized).

Checking a Font

The "fontValid" method of the FontManager returns zero or one if the specified font is invalid or valid (installed on the current system). For example, the following checks to see whether the font 9x15 is installed on your system:

if [::tycho::.tychoFonts fontValid 9x15] {
    ::tycho::post {The 9x15 font is available}
} {
    ::tycho::post {The 9x15 font is not available}
}

Getting a Font from a Specification

The "getFont" method takes as arguments a font specification, and checks to see whether the font is available on the current system. If it is, it return the font name. Otherwise, return the empty string. A font specification consists of four items, the family, point size, weight, and style. Common families are "times", "courier", and "helvetica". Reasonable point sizes are 12, 14, 18, and 24. The weight is typically either "medium" or "bold", and the style is either "r" or "i" for roman or italic.

set font [::tycho::.tychoFonts getFont times 18 medium i]
if {$font != {}} {
    ::tycho::post "The font name is: $font"
} {
    ::tycho::post {No font was found}
}

Finding a Valid Font

Frequently, applications will wish to search through some set of preferred fonts before resorting to the only font guaranteed to exist on all systems, "fixed". The method "findFont" takes a list of specifications and tries to find a font that matches one in the list. Specifications earlier in the list are given priority. A specification is either a font name or a list with four items. The items are the family, point size, weight, and style. These items have the same meaning as that used in the "getFont" method. For example, the following code will try to use the 9x15 font, but if it is not installed, it will try to use a courier font, and if that is not installed, it will resort to "fixed".

::tycho::post [
    ::tycho::.tychoFonts findFont {
         9x15
         {courier 14 medium r}
    }
]
The "findFont" method always returns a valid font name.

Default Fonts

The "defaultFont" method returns the name of a preferred font from a list of possibilities. The possible options are fixed, fixedBold, fixedItalic, variable, variableBold, and variableItalic. For example, to identify the font that Tycho is using as its default fixed-width italic font, issue the following command

::tycho::post [
    ::tycho::.tychoFonts defaultFont fixedItalic
]


Copyright © 1996, The Regents of the University of California. All rights reserved.
Last updated: 96/04/09, comments to: eal@eecs.berkeley.edu